babl-palette: warn when trying to create a 0-color palette
authorEll <ell_se@yahoo.com>
Wed, 30 May 2018 10:01:27 +0000 (06:01 -0400)
committerEll <ell_se@yahoo.com>
Wed, 30 May 2018 10:04:42 +0000 (06:04 -0400)
In babl_palette_set_palette(), fall back to the default palette,
and log a warning, when trying to create a palette with < 1 colors.

babl/babl-palette.c

index 5b19c0aaa10e25fe14c9dad82676b729e246aef9..278c854fe48b48572bd4cb5a604cb156338bf38a 100644 (file)
@@ -244,6 +244,8 @@ static BablPalette *make_pal (const Babl *format, const void *data, int count)
   BablPalette *pal = NULL;
   int bpp = babl_format_get_bytes_per_pixel (format);
 
+  babl_assert (count > 0);
+
   pal = babl_malloc (sizeof (BablPalette));
   pal->count = count;
   pal->format = format;
@@ -870,7 +872,16 @@ babl_palette_set_palette (const Babl *babl,
 {
   BablPalette **palptr = babl_get_user_data (babl);
   babl_palette_reset (babl);
-  *palptr = make_pal (format, data, count);
+  if (count > 0)
+    {
+      *palptr = make_pal (format, data, count);
+    }
+  else
+    {
+      babl_log ("attempt to create a palette with %d colors. "
+                "using default palette instead.",
+                count);
+    }
 }
 
 void